package transaction;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 事务消息-消费者
 *
 * @author Administrator
 * @date 2022/03/31 14:08
 **/
@Slf4j
public class TransactionConsumer {
    public static void main(String[] args)throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Transaction_Consumer");
        consumer.setNamesrvAddr("rocketmq1.cn:9876;rocketmq2.cn:9876");
        // topic tag
        consumer.subscribe("Transaction-Msg","*");
        // 回调函数
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {


                for (MessageExt msg : msgs) {
                    log.info("线程:{} , 消息:{} tag:{}", Thread.currentThread().getName(), new String(msg.getBody()),msg.getTags());
                }

                // 返回消费成功
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        // 开始消费
        consumer.start();
        log.info("事务消息消费开始");
    }
}
